import os
import subprocess

# 定义git仓库列表，格式为：[("仓库地址", "分支", {"额外参数": "值"}), ...]
# 如果分支为空字符串，则拉取默认分支
# 额外参数为字典形式，支持自定义命令行参数
repositories = [
    ("https://gitee.com/hyqf98/spring-cloud-netflix.git", "source-read-v2.2.6.RELEASE", {}),
    ("https://gitee.com/hyqf98/spring-ai.git", "", {"env": {"GIT_LFS_SKIP_SMUDGE": "1"}}),
    ("https://gitee.com/hyqf98/spring-cloud-alibaba.git", "source-read-v2.2.6.RELEASE", {}),
    ("https://gitee.com/hyqf98/spring-cloud-commons.git", "source-read-v2.2.6.RELEASE", {}),
    ("https://gitee.com/hyqf98/spring-cloud-openfeign.git", "source-read-2.2.6", {}),
    ("https://gitee.com/hyqf98/nacos.git", "source-read-2.0.0", {}),
    ("https://gitee.com/hyqf98/rocketmq.git", "source-read-5.0.0-alpha", {}),
    ("https://gitee.com/hyqf98/mybatis-3.git", "source-read-3.5.6", {}),
    ("https://gitee.com/hyqf98/netty.git", "source-read-4.1.78.Final", {}),
    ("https://gitee.com/hyqf98/hikariCp.git", "source-read-4.0.3", {}),
    ("https://gitee.com/hyqf98/seata.git", "source-read-1.5.0", {}),
    ("https://gitee.com/hyqf98/canal.git", "source-read-canal-1.1.7", {}),
    ("https://gitee.com/hyqf98/spring-cloud-gateway.git", "source-read-3.1.9", {}),
    ("https://gitee.com/hyqf98/dubbo.git", "source-read-dubbo-3.2.7", {}),
    ("https://gitee.com/hyqf98/jackson.git", "source-read-2.16.0", {}),
    ("https://gitee.com/hyqf98/spring-cloud-stream.git", "", {}),
    ("https://gitee.com/hyqf98/spring-cloud-function.git", "", {}),
    ("https://gitee.com/hyqf98/slf4j.git", "", {}),
]

def clone_or_update_repo(repo_url, branch, extra_params):
    repo_name = repo_url.split("/")[-1].replace(".git", "")
    if os.path.exists(repo_name):
        print(f"🔄 Updating {repo_name}...")
        os.chdir(repo_name)
        subprocess.run(["git", "pull"])
        os.chdir("..")
    else:
        print(f"📥 Cloning {repo_name}...")
        command = ["git"]
        env = os.environ.copy()
        if "env" in extra_params:
            env.update(extra_params["env"])
        if branch:
            command.extend(["clone", "-b", branch, repo_url])
        else:
            command.extend(["clone", repo_url])
        subprocess.run(command, env=env)

def main():
    for repo_url, branch, extra_params in repositories:
        clone_or_update_repo(repo_url, branch, extra_params)

if __name__ == "__main__":
    main()
